package com.shj.em.aa.controller;

import com.shj.em.aa.dto.RoleUsersDTO;
import com.shj.em.aa.dto.TransferDTO;
import com.shj.em.aa.dto.UserRoleResDTO;
import com.shj.em.aa.entity.Resource;
import com.shj.em.aa.entity.Role;
import com.shj.em.aa.entity.User;
import com.shj.em.aa.service.*;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.KeyLabelVO;
import com.shj.em.common.annotation.Log;
import com.shj.em.common.to.Tree;
import com.shj.em.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-06
 */
@RestController
@Slf4j
@RequestMapping("/aa/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private RoleResService roleResService;

    @GetMapping("/gotoAddUser2Role/{roleId}")
    public ResMsg gotoAddUser2Role(@PathVariable("roleId") Long roleId){
        List<User> users = userService.getUsersByRoleId(roleId);
        List<User> allUsers = userService.getAllUsers();
        TransferDTO transferDTO = new TransferDTO();
        transferDTO.setAssigned(convert(users));
        transferDTO.setAll(convert(allUsers));

        return ResMsg.ok(transferDTO);
    }

    private List<KeyLabelVO> convert(List<User> users){
        if(users == null){
            return null;
        }

        List<KeyLabelVO> list = new ArrayList<>();
        KeyLabelVO keyLabelVO;

        for(User user : users){
            keyLabelVO = new KeyLabelVO();
            keyLabelVO.setKey(user.getId() + "");
            keyLabelVO.setLabel(user.getUsername());
            list.add(keyLabelVO);
        }
        return list;
    }

    @Log("给角色分配用户")
    @PostMapping("/assignUsers2Role")
    public ResMsg assignUsers2Role(@RequestBody RoleUsersDTO roleUsersDTO){
        userRoleService.updateRoleUserMapping(roleUsersDTO.getRoleId(), roleUsersDTO.getUsers());
        return ResMsg.ok();
    }

    @GetMapping("/gotoAddRes2Role/{roleId}")
    public ResMsg gotoAddRes2Role(@PathVariable("roleId") Long roleId){
        List<Resource> assingedRes = resourceService.findResByRoleId(roleId);
        List<Resource> allRes = resourceService.findAllRes();
        TransferDTO transferDTO = new TransferDTO();
        transferDTO.setAssigned(convertRes(assingedRes));
        transferDTO.setAll(convertRes(allRes));

        return ResMsg.ok(transferDTO);
    }

    private List<KeyLabelVO> convertRes(List<Resource> resources){
        if(resources == null){
            return null;
        }

        List<KeyLabelVO> list = new ArrayList<>();
        KeyLabelVO keyLabelVO;

        for(Resource res : resources){
            keyLabelVO = new KeyLabelVO();
            keyLabelVO.setKey(res.getId() + "");
            keyLabelVO.setLabel(res.getName());
            list.add(keyLabelVO);
        }
        return list;
    }

    @Log("给角色分配资源")
    @PostMapping("/assignRes2Role")
    public ResMsg assignRes2Role(@RequestBody RoleUsersDTO roleUsersDTO){
        roleResService.updateRoleResMappings(roleUsersDTO.getRoleId(), roleUsersDTO.getResources());
        return ResMsg.ok();
    }

    @GetMapping("/gotoAddRole2User/{userId}")
    public ResMsg gotoAddRole2User(@PathVariable("userId") Long userId){
        List<Role> roles = roleService.findRolesByUserId(userId);
        List<Role> allRoles = roleService.findAllRoles();
        TransferDTO transferDTO = new TransferDTO();
        transferDTO.setAssigned(convertRole(roles));
        transferDTO.setAll(convertRole(allRoles));

        return ResMsg.ok(transferDTO);
    }

    private List<KeyLabelVO> convertRole(List<Role> roles){
        if(roles == null){
            return null;
        }

        List<KeyLabelVO> list = new ArrayList<>();
        KeyLabelVO keyLabelVO;

        for(Role role : roles){
            keyLabelVO = new KeyLabelVO();
            keyLabelVO.setKey(role.getId() + "");
            keyLabelVO.setLabel(role.getRoleName());
            list.add(keyLabelVO);
        }
        return list;
    }

    @Log("给用户分配角色")
    @PostMapping("/assignRoles2User")
    public ResMsg assignRoles2User(@RequestBody UserRoleResDTO userRoleResDTO){
        userRoleService.updateUserRoleMapping(userRoleResDTO.getUserId(), userRoleResDTO.getRoles());
        return ResMsg.ok();
    }



    @Log("获得用户拥有的菜单")
    @GetMapping("/getMenus")
    ResMsg getMenus(){
        List<Tree<Resource>> menus = resourceService.getMenus(ShiroUtils.getUserId());
        /*Map<String, Object> model = new HashMap<>();
        model.put("menus", menus);
        model.put("picUrl","/img/default_profile.jpg");*/
        return  ResMsg.ok(menus);
    }

}
